#gem "C:/Ruby187/lib/ruby/gems/1.8/gems/googlecharts-1.6.8/lib/googlecharts", :require => "gchart"


class StatsController < ApplicationController

  def index
    begin
        require "gchart"
        #recuperer les date entrees dans le formulaire
        if(params['dated'])
          @date_debut = Date.new(params[:dated][:"start_date(1i)"].to_i,params[:dated][:"start_date(2i)"].to_i,params[:dated][:"start_date(3i)"].to_i)
          #@date_debut = @date_debut1.to_date
          @date_fin = Date.new(params[:datef][:"end_date(1i)"].to_i,params[:datef][:"end_date(2i)"].to_i,params[:datef][:"end_date(3i)"].to_i)
          
          @date_all = @date_fin-@date_debut
          #une date fin < a une date de debut ne retournera rien
          
          if(@date_all <= 0 )
            flash[:info] = 'Votre date de fin n\'est pas valide.'
            @error = 'error'
          end

          #if(@date_all >= 90 )
          #  flash[:notice] = 'La période que vous avez saisie est très grande'
          #  @error = 'eeror'
          #else 
            
          # ADD sortir de la methode
          #raise RuntimeError, 'Message goes here'
          #abort("Votre date de fin n\'est pas valide.")
        end
      
        #recuperation des stats generales total
        @nb_rooms = Room.count
        @nb_computers = Computer.count
        @avg_comp_room = (Float(Computer.count) / Float(Room.count)).round(2)
        
        #temps moyen passe pour la résolution d'incidents
        @avg_resolved_events_all = Event.all( :conditions => ["created_at != updated_at"], 
                                      :select => [" (AVG(UNIX_TIMESTAMP(updated_at) - UNIX_TIMESTAMP(created_at))) as periodbetween "])
      
        #@aa = 1.week.ago
        if(params['dated'] && @date_all > 0)
          @comp = 0
            @events_last_week_nresolved = Event.count(:conditions => ["state = 1 AND created_at > ? AND created_at < ? ",   @date_debut, @date_fin])
            @events_last_week_resolved = Event.count(:conditions => ["state = 0 AND created_at > ? AND created_at < ? ",  @date_debut, @date_fin])
            @avg_resolved_events_week = Event.all( :conditions => ["created_at != updated_at AND created_at > ? AND created_at < ? ",  @date_debut, @date_fin], 
                            :select => [" (AVG(UNIX_TIMESTAMP(updated_at) - UNIX_TIMESTAMP(created_at))) as periodbetween "]) 
            case params['period_id'].to_i
              when 1
                @end_comp = @date_all
              when 2
                @end_comp = @date_all.to_i / 7
              when 3
                @end_comp = @date_all.to_i / 30
            end
        else
          #recupérer les evenemts de de maintenant a une semaine avant             
          @comp = 0
          @date_debut = 5.week.ago
          @date_fin = 0.week.ago
          @end_comp = 5
          @events_last_week_nresolved = Event.count(:conditions => ["state = 1 AND created_at > ? AND created_at < ? ",  @date_debut, @date_fin])
          @events_last_week_resolved = Event.count(:conditions => ["state = 0 AND created_at > ? AND created_at < ? ",  @date_debut, @date_fin])
          @avg_resolved_events_week = Event.all( :conditions => ["created_at != updated_at AND created_at > ? AND created_at < ? ",  @date_debut, @date_fin], 
                                      :select => [" (AVG(UNIX_TIMESTAMP(updated_at) - UNIX_TIMESTAMP(created_at))) as periodbetween "])
        end
        @date_w_d = @date_debut
        @date_w_f = @date_fin
        @format = '%d-%m-%Y' 
        @date_debut = @date_debut.strftime(@format) 
        @date_fin = @date_fin.strftime(@format) 
        
        @events_by_week_nsolved = []
        @events_by_week_solved = []
        @total_period =  []
        @labels_x_date = []
    @po = 0
     @po1 = 2
         @hg = @date_w_d + (@po * 7)
        @hg1 = @date_w_d + (@po1 * 7)
        
        while @comp < @end_comp
         @total_period << @comp
         @compAfter = @comp + 1
         if(params['dated'] && @date_all > 0)
            case params['period_id'].to_i
              when 1
                 @events = Event.count(:conditions => ["state = 1 AND created_at > ? AND created_at < ? ", @date_w_d + @comp, @date_w_d + @compAfter])
                 @eventsn = Event.count(:conditions => ["state = 0 AND created_at > ? AND created_at < ? ", @date_w_d + @comp, @date_w_d + @compAfter])
                 
                 if(@events > 0 || @eventsn > 0)
                  @events_by_week_solved << @events
                  @events_by_week_nsolved << @eventsn
                  
                  #@lab_x = Event.all(:conditions => ["state = 0 AND created_at < ? AND created_at > ? ", @comp.day.ago, @compAfter.day.ago], :select => ["created_at"])
                  
                  #@lab_x.each do |@l| 
                    #@date_debut = @date_debut.strftime(@format)
                    # @labels_x_date << (@l.created_at).strftime(@format)
                  #end
                 end
              when 2
                 @events = Event.count(:conditions => ["state = 1 AND created_at > ? AND created_at < ? ", @date_w_d + (@comp * 7), @date_w_d + (@compAfter * 7)])
                 @eventsn = Event.count(:conditions => ["state = 0 AND created_at > ? AND created_at < ? ", @date_w_d + (@comp * 7), @date_w_d + (@compAfter * 7)])
                 if(@events > 0 || @eventsn > 0)
                  @events_by_week_nsolved << @eventsn
                  @events_by_week_solved << @events
                 end
              when 3 
                @events = Event.count(:conditions => ["state = 1 AND created_at > ? AND created_at < ? ", @date_w_d + (@comp * 30), @date_w_d + (@compAfter * 30)])
                @eventsn = Event.count(:conditions => ["state = 0 AND created_at > ? AND created_at < ? ", @date_w_d + (@comp * 30), @date_w_d + (@compAfter * 30)])
                if(@events > 0 || @eventsn > 0)
                 @events_by_week_nsolved << @eventsn
                 @events_by_week_solved << @events
                end
            end
         else
            @events = Event.count(:conditions => ["state = 1 AND created_at < ? AND created_at > ? ", @comp.week.ago, @compAfter.week.ago])
            @eventsn = Event.count(:conditions => ["state = 0 AND created_at < ? AND created_at > ? ", @comp.week.ago, @compAfter.week.ago])
            if(@events > 0 || @eventsn > 0)
              @events_by_week_nsolved << @eventsn
              @events_by_week_solved << @events
            end
         end
         @comp +=1
        end
        if @events_by_week_solved.empty? || @events_by_week_solved.nil? 
          @events_by_week_solved << 0
        end
        if @events_by_week_nsolved.empty? || @events_by_week_nsolved.nil? 
          @events_by_week_nsolved << 0
        end
         @max1 = @events_by_week_solved.max
         @max2 = @events_by_week_nsolved.max
        if @max1 >= @max2
          @max = @max1
        else
          @max = @max2
        end
        
         #@to = []
         #@comp2= 0
         #@ff = @events_by_week_nsolved.count
        #while @comp2 < @events_by_week_nsolved.count
       #  @to << @comp2.to_s
       #  @comp2 +=1
        #end
        
        
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @events_by_week }
        end
      rescue
       flash[:info] = 'Votre date n\'est pas valide.'
       @error = "error"
      end
   end
  
  def statroom
        @events_temp = Room.find(params[:id],
                      :joins => " RIGHT JOIN computers ON rooms.id = computers.room_id
                        LEFT JOIN events ON computers.id = events.computer_id",
                  :from => "rooms",
                  :select => "rooms.id AS room_id, count(events.id) AS count",
                  :conditions => "events.state = 0 OR computers.state_computer = 0 OR computers.state_agent = 0",
                  :group => "rooms.id"
                 )
  end
  

  
  def statevents
    
  end
  
end
